package de.hpi.generator.config;

import java.util.ArrayList;

/**
 * Database configuration model
 * @author Michael Wolowyk
 */
public class DatabaseModel {


    public String name;
    public ArrayList<TableModel> tables = new ArrayList<TableModel>();
    public DependencyModels InclusionDependencies = new DependencyModels();

    public DatabaseModel(String name) {
        this.name = name;

    }

    /**
     * add table model if not exists in the collection
     * @param tableModel
     * @throws ExistsException 
     */
    public void addTableModel(TableModel tableModel) throws ExistsException {
        if (findTableModelByName(tableModel.name) == null) {
            this.tables.add(tableModel);
        } else {
            throw new ExistsException("The table " + tableModel.name + " is already exists");
        }
    }

    /**
     * finds table model by name
     * @param aTableModelName
     * @return a table model
     */
    public TableModel findTableModelByName(String aTableModelName) {
        TableModel result = null;
        for (TableModel aTestObject : this.tables) {
            if (aTestObject.name.compareTo(aTableModelName) == 0) {
                result = aTestObject;
            }
        }
        return result;
    }

    @Override
    public String toString() {
        String result = String.format("{name: %s, \n TableModels:[ \n", this.name);
        String aTableModelsString = "";
        String aInclusionDependencyModelString = "";
        for (TableModel aTableModel : tables) {
            aTableModelsString += "\t" + aTableModel.toString();
        }
        for (DependencyModel aInclusionDependencyModel : InclusionDependencies) {
            aInclusionDependencyModelString += "\t" + aInclusionDependencyModel.toString();
        }
        return result + aTableModelsString + "] \n InclusionDependencies:[ \n" + aInclusionDependencyModelString + "]}\n";
    }
}
